<?php
session_start();
if (isset($_SESSION['userid']) && $_SESSION['level']!=2)
{
	header ("location: login.php");
	exit();
}
?>
<?php
include("includes/top_user.php");
?>

<?php
include("includes/connect.php");
?>

<?php
$id=$_GET['threadid'];
$sql = "select thread.id as ma_baiviet, thread.title, thread.content,thread.date, user.username as author, user.id as ma_user, thread.madanhmuc as ma_danhmuc, category.cate_name from thread,user,category where thread.id='".$id."'";
$query = mysql_query($sql);
$row = mysql_fetch_array($query);

?>
<?php
if(isset($_POST['ok']))
{	
	if($_POST['tb_title'] == null) { echo "Please enter your title"; }
	else{ $t = $_POST['tb_title'];}
	
	if($_POST['ckeditor'] == null) { echo "Please enter your content"; }
	else{ $c = $_POST['ckeditor']; }
	
	$da = $_POST['danhmuc'];
	
	if($_POST['tb_date'] == null){ echo "Please enter your date";}
	else { $d = $_POST['tb_date'];}
	
	if($t && $c && $da && $d)
	{
		$sql1 = "update thread set title='".$t."', content='".$c."', madanhmuc='".$da."',  date='".$d."' where thread.id='".$id."'";
		mysql_query($sql1);
		header("location:thread_manager.php");
		exit();
 	}
}
?>
<div id="box">

<h3>Quản lý bài viết</h3>
<form class="fr_manage_thread" name="fr_manage_thread" action="edit_thread1.php?threadid=<?=$id?>" method="post">
	<table>
    <tr>
    <td>Mã bài viết :</td>
    <td> <input type="text" name="mabaiviet" value="<?=$row['ma_baiviet']?>" /></td>
    </tr>
    <tr>
    <td>Tiêu đề :</td>
    <td> <input type="text" name="tb_title" value="<?=$row['title']?>"  /></td>
    </tr>
    <tr>
	<td>Nội dung :</td><td>&nbsp;</td>
    </tr>
    <tr>
    <td colspan="2">
     <textarea class="ckeditor" name="ckeditor" rows="100" cols="100"><?=$row['content']?>
     </textarea>
     </td>
     </tr>
     <tr>
    <td>Tác giả : </td><td><input type="text" name="tb_author" value=<?=$row['author'] ?> /></td>
    </tr>
    <tr>
    <td>Thuộc danh mục :</td>
    <td> <select name="danhmuc">
    					<?php
						$sql_category = "select * from category";
						mysql_query("set names 'utf8'");
						$query_category =mysql_query($sql_category);
						while($row_category = mysql_fetch_array($query_category))
						{
							if($row['ma_danhmuc'] == $row_category['id'])
							{
								echo "<option value=$row_category[id] selected>$row_category[cate_name]</option>";
							}
							else
							{
								echo "<option value=$row_category[id]]>$row_category[cate_name]</option>";
							}
						}
                        ?>
    				</select>
    </td>
    <tr>
   <td> Ngày viết :</td> <td><input type="text" name="tb_date" value="<?=$row['date']?>"  /></td> </tr>
   <tr>
   <td>&nbsp;</td>
    <td><input type="submit" name="ok" value="Cập Nhật"  /></td>
    </tr>
   </table>  
</form>

</div><!--box-->

<?
include("includes/sidebar.php");
include("includes/footer.php");
?>

